package cn.lopr.im.android.mvp.view.activities;

import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticlePlate;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.databinding.ActivityArticlePublishBinding;
import cn.lopr.im.android.mvp.contact.IArticlePublishContact;
import cn.lopr.im.android.mvp.presenter.ArticlePublishPresenterImpl;
import cn.lopr.im.android.mvp.view.adapters.CommunityPlateAdapter;
import cn.lopr.im.android.mvp.view.adapters.ImageAdapter;


public class ArticlePublishActivity extends Activity implements IArticlePublishContact.IArticlePublishView {

    private ActivityArticlePublishBinding binding;
    private String actionAdd = "add";
    private String actionUpdate = "update";
    private String currentAction;
    private IArticlePublishContact.IArticlePublishPresenter articlePublishPresenter;
    private User user;
    private List<ArticlePlate> articlePlates;
    private CommunityPlateAdapter communityPlateAdapter;
    private String currentPlateId;
    private List<Integer> img = new ArrayList<Integer>() {
        {
            add(1);
        }
    };
    private ImageAdapter imageAdapter;
    private String articleId;
    private ArticleVo articleVo;

    @Override
    protected View getContentLayoutId() {
        binding = ActivityArticlePublishBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        this.user = (User) intent.getSerializableExtra("user");
        this.currentAction = intent.getStringExtra("action");
        this.articlePublishPresenter = new ArticlePublishPresenterImpl(this);
        this.articlePublishPresenter.getPlate(this.getToken(this.getApplicationContext()));

        if (actionUpdate.equals(currentAction)) {
            this.binding.includeAppbar.ivEdit.setVisibility(View.VISIBLE);
            this.binding.includeAppbar.tvPublish.setText("修改");
            this.binding.includeAppbar.tvTitle.setText("修改动态");
            this.articleId = intent.getStringExtra("articleId");
            this.articlePublishPresenter.getArticle(this.getToken(this.getApplicationContext()));
        } else {
            this.binding.includeAppbar.ivEdit.setVisibility(View.GONE);
            this.binding.includeAppbar.tvPublish.setVisibility(View.VISIBLE);
            this.binding.includeAppbar.tvPublish.setText("发表");
            this.binding.includeAppbar.tvTitle.setText("发表动态");
        }

        this.imageAdapter = new ImageAdapter(this.getApplication(), this.img, getPixelsFromDp(130));
        Objects.requireNonNull(this.binding.gvImg).setAdapter(this.imageAdapter);
    }


    @Override
    protected void initWidget() {
        super.initWidget();

        Objects.requireNonNull(binding.includeAppbar.ivBack).setOnClickListener(e -> this.finish());
        Objects.requireNonNull(binding.includeAppbar.tvPublish).setOnClickListener(v -> {
            showLoading(this);
            if (actionUpdate.equals(currentAction)) {
                articlePublishPresenter.updArticle(this.getToken(this.getApplicationContext()));
            } else {
                articlePublishPresenter.addArticle(this.getToken(this.getApplicationContext()), user);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext()); //LinearLayoutManager中定制了可扩展的布局排列接口，子类按照接口中的规范来实现就可以定制出不同排雷方式的布局了
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        Objects.requireNonNull(this.binding.communityPlateList).setLayoutManager(layoutManager);
    }

    @Override
    public String getContent() {
        return Objects.requireNonNull(binding.etContent).getText().toString();
    }

    @Override
    public void getPlatesSuccess(List<ArticlePlate> articlePlates) {
        articlePlates.get(0).setCurrent(true);
        this.articlePlates = articlePlates;
        this.currentPlateId = this.articlePlates.get(0).getId();
        this.communityPlateAdapter = new CommunityPlateAdapter(this.getApplicationContext(), articlePlates);
        this.binding.communityPlateList.setAdapter(this.communityPlateAdapter);
        this.communityPlateAdapter.onRecyclerViewItemClickListener(new CommunityPlateAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int section) {
                ArticlePlate articlePlate = (ArticlePlate) view.getTag();
                if (view.getId() == R.id.tv_community_plate) {
                    updatePlateState(articlePlate.getId());
                }
            }

            @Override
            public void onItemLongClick(View view, int section) {

            }
        });
        if (this.articleVo != null) {
            for (ArticlePlate articlePlate : Objects.requireNonNull(this.articlePlates)) {
                articlePlate.setCurrent(articlePlate.getId().equals(this.articleVo.getPlateId()));
            }
        }
    }

    @Override
    public void getArticleSuccess(ArticleVo articleVo) {
        binding.etContent.setText(articleVo.getContent());
        this.articleVo = articleVo;
        for (ArticlePlate articlePlate : Objects.requireNonNull(this.articlePlates)) {
            if (articlePlate.getId().equals(articleVo.getPlateId())) {
                articlePlate.setCurrent(true);
                this.currentPlateId = articlePlate.getId();
                break;
            }
            articlePlate.setCurrent(false);
        }
        this.communityPlateAdapter.refresh(this.articlePlates);
    }

    @Override
    public void addArticleSuccess() {
        this.finish();
    }

    @Override
    public void updArticleSuccess() {
        this.finish();
    }

    @Override
    public String getPlateId() {
        return this.currentPlateId;
    }

    @Override
    public String getArticleId() {
        return this.articleId;
    }

    /**
     * 更新板块状态 设置当前板块ID
     *
     * @param currentPlateId 当前板块id
     */
    void updatePlateState(String currentPlateId) {
        this.currentPlateId = currentPlateId;
        for (ArticlePlate articlePlate : this.articlePlates) {
            articlePlate.setCurrent(articlePlate.getId().equals(currentPlateId));
        }

        this.communityPlateAdapter.refresh(this.articlePlates);
    }

    private int getPixelsFromDp(int size) {

        DisplayMetrics metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        return (size * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;

    }
}